window.$ = require("jquery")
$ ->
    window.navigator.serviceWorker.register('/sw.js')

import 'animate.css/animate.min.css'
import VueRouter from 'vue-router'
import Vue from "vue/dist/vue.js"


`const AsyncFunction = (async ()=>{}).constructor`

async_data = (mod)->
    data = mod.data
    _ = undefined


    beforeRouteEnter = mod.beforeRouteEnter

    if beforeRouteEnter
        mod.beforeRouteEnter = (from, to, next)->
            _ = await data()
            beforeRouteEnter.apply(@,arguments)
    else
        mod.beforeRouteEnter = (from, to, next)->
            _ = await data()
            next()

    mod.data = -> _

Vue.use(VueRouter)
window.GO = new VueRouter({
    mode: 'history'
    routes: do ->
        module = require './.import'
        _routes = []
        for k,v of require('./route')
            _routes.push {
                path:v
                component:=>
                    mod = (await module[k]).default
                    data = mod.data
                    if data instanceof AsyncFunction
                        async_data(mod)



                    mod
            }
        _routes
})

window.VUE = new Vue({
    router:GO
}).$mount('#VUE')

    #beforeRouteEnter:(from, to, next)->
    #    next()
